/*
 * Copyright 2009 Al Phillips.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package xsd4gorm.gen

import xsd4gorm.model.DomainClass
import xsd4gorm.model.DomainType

/**
 *
 * @author Al Phillips
 */
public class ClassSourceGenerator {

    protected static final String TAB = "    "
    private String source = ""
    private DomainClass domainClass

    public ClassSourceGenerator(DomainClass domainClass){
        this.domainClass = domainClass 
    }

    public String generate(){
        sourceCodeln "class ${domainClass.getName()} {\n"
        domainClass.getTypes().each {type ->
            generateType type
        }
        generateConstraints()
        generateHasMany()
        sourceCode "\n}"
        getSourceCode()
    }

    private void generateType(DomainType type){
        if (type.getHasMany()){
            domainClass.getHasManyMap().put(type.getName(), type.getType())
        } else {
            sourceCodeln TAB + "${type.getType()} ${type.getName()}"
        }
    }

    

    private void generateHasMany(){
        if(domainClass.getHasManyMap().size() > 0){
            // TODO: May need to consider this approach if there are two many items in hasMany
            /*sourceCode += TAB +     "static hasMany = ["
            int count = 1
            hasManyMap.each { entry ->
                sourceCode += "${entry.key}:${entry.value}"
                if (count < hasManyMap.size()){
                    sourceCode += ",\n${TAB}                  "
                }
                count ++
            }
            sourceCode += "]\n"*/
            sourceCode TAB +     "static hasMany = ${domainClass.getHasManyMap().toMapString()}"
            sourceCodeln TAB
        }
    }

    private void generateConstraints(){

        def constraintDef = []
        domainClass.getTypes().each {type ->
            if (type.getConstraints().size() > 0){
                def conName = "${type.getName()}(\n"
                type.getConstraints().eachWithIndex {con, i ->
                    conName += TAB + TAB + TAB + "${con.key}:${con.value}"
                    if (i < type.getConstraints().size()-1){
                        conName += ",\n"
                    } else {
                        conName += "\n"
                    }
                }
                conName += TAB + TAB + ")"
                constraintDef << conName
            }
        }

        sourceCodeln TAB + "static constraints = {"
        constraintDef.each {constraint ->
            sourceCodeln TAB + TAB + constraint
        }
        sourceCodeln TAB + "}"
    }

    protected void sourceCodeln(source){
        this.source += source + "\n"
    }

    protected void sourceCode(source){
        this.source += source
    }

    protected String getSourceCode(){
        this.source
    }
}